/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bs.coregamelibrary.session;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * @author dungpt
 */
public abstract class SessionPool {

    protected ConcurrentHashMap<String, Session> pool = new ConcurrentHashMap<>();

    public abstract void loadSessionFromDatabase();

    public abstract <T extends Session> T getSessionFromPool(String sUserID);

    public abstract boolean updateSession(String sUserID, ChannelHandlerContext ctx);

    public abstract boolean updateSession(String sUserID, Channel ctx);

    public abstract boolean updateSession(String sUserID, Session session);

    public abstract boolean removeSession(String sUserID);

    public void removeAll() {
        this.pool.clear();
    }

    public void broadcastAll(Object message) throws IOException {
        List<Session> list = new ArrayList<>(this.pool.values());
        for (Session session : list) {
            session.sendMessage(message);
        }
    }

    public void broadcastAllExceptMe(Session me, Object message) throws IOException {
        List<Session> list = new ArrayList<>(this.pool.values());
        for (Session session : list) {
            if (session != me) {
                session.sendMessage(message);
            }
        }
    }

}
